<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    input {
      width: 30px;
    }
  </style>
</head>

<body>

  <div id="app">
    姓：<input type="text" v-model="firstName"> +
    名：<input type="text" v-model="lastName"> =
    <span>{{ fullName }}</span><br><br>

    <button @click="change">改名卡</button>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        firstName: '刘',
        lastName: '备',
      },
      methods: {
        change() {
          this.fullName = '黄忠'
        }
      },
      computed: {
        // 完整写法:既可以获取,也可以修改
        fullName:{
          //获取,当访问这个计算属性时,get幻术会自动执行,并将返回值作为计算属性的值
          //这个get 方法就是简单写法的函数
          get() {
            return this.firstName + this.lastName
          },
          //设置,当修改这个计算属性时,set函数会自动执行,并将新值作为参数传递过来
          set(val){
            //console.log(val)
            //将姓和名切割出来分别赋值给 firstName 和 lastName
            this.firstName=val.substring(0,1)
            this.lastName=val.substring(1)
          }
        }
        //简单写法:可以获取来使用,但不能被修改
        // fullName() {
        //   return this.firstName + this.lastName
        // }
      }
    })
  </script>
</body>

</html>